package org.jeecg.modules.solitaire_app.app.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefinery;
import org.jeecg.modules.solitaire_app.app.mapper.YsyRefineryMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyRefineryService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 炼厂表
 * @Author: jeecg-boot
 * @Date: 2023-08-29
 * @Version: V1.0
 */
@Service
@DS("ysy_db")
public class YsyRefineryServiceImpl extends ServiceImpl<YsyRefineryMapper, YsyRefinery> implements IYsyRefineryService {

    @Value("${tengcent.apiMap}")
    private String apiMap;

    @Override
    public Result<List<YsyRefinery>> getRefineryListByMap(String address) {
        Result<List<YsyRefinery>> result = new Result<>();
        List<YsyRefinery> ysyRefineries = new ArrayList<>();
        try {
            String url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=" + this.apiMap;
            String jsonStr = HttpUtil.get(url);
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            if (jsonObject.getInteger("status") == 0) {
                JSONObject json = jsonObject.getJSONObject("result").getJSONObject("location");
                ysyRefineries = this.baseMapper.getRefineryListByMap(json.getString("lat"), json.getString("lng"));

                if (ysyRefineries.size() == 0) {
                    //新增一个炼厂
                    YsyRefinery ysyRefinery = new YsyRefinery();
                    ysyRefinery.setRefineryName("未知炼厂");
                    ysyRefinery.setLat(json.getString("lat"));
                    ysyRefinery.setLng(json.getString("lng"));
                    ysyRefinery.setCity("未知");
                    ysyRefinery.setContent("暂无简介");
                    ysyRefinery.setAddress(address);
                    ysyRefinery.setStatus("0");
                    ysyRefinery.setCreateTime(new Date());
                    ysyRefinery.setUpdateTime(new Date());
                    this.baseMapper.insert(ysyRefinery);

                    ysyRefineries.add(ysyRefinery);
                }
            } else {
                result.setCode(500);
                result.setMessage("您输入的地址不正确！！！");
                result.setSuccess(false);
                return result;
            }
            result.setCode(200);
            result.setResult(ysyRefineries);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }

    }

    @Override
    @Transactional
    public Result<?> updateRefinery(YsyRefinery ysyRefinery) {
        Result<?> result = new Result<>();
        try {
            String url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + ysyRefinery.getAddress() + "&key=" + this.apiMap;
            String jsonStr = HttpUtil.get(url);
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            if (jsonObject.getInteger("status") == 0) {
                JSONObject json = jsonObject.getJSONObject("result").getJSONObject("location");
                ysyRefinery.setLat(json.getString("lat"));
                ysyRefinery.setLng(json.getString("lng"));
            }
            ysyRefinery.setUpdateTime(new Date());
            this.baseMapper.updateRefinery(ysyRefinery);
            result.setCode(200);
            result.setMessage("修改成功");
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }
}
